package com.rran.study.designpattern.behavioral.chain.factory;

import com.rran.study.designpattern.behavioral.chain.chain.AbstractAttackChain;
import com.rran.study.designpattern.behavioral.chain.chain.ChainHandler;
import com.rran.study.designpattern.behavioral.chain.enums.AttackEnums;

/**
 * @author yy
 * @Type ChainEnumFactory.java
 * @Desc
 * @date 2020/7/20 15:07
 */
public class ChainEnumFactory {


    /**
     * 反射实体化具体的处理者
     *
     * @param attackEnum
     * @return
     */
    public static ChainHandler getChainHandler(AttackEnums attackEnum) {
        try {
            Integer handlerId = attackEnum.getHandlerId();
            Integer next = attackEnum.getNext();
            Boolean isHead = attackEnum.getIsHead();
            Class<?> clazz = Class.forName(attackEnum.getClassPath());
            AbstractAttackChain chain =  (AbstractAttackChain) clazz.newInstance();
            chain.setNext(next);
            chain.setHead(isHead);
            chain.setHandlerId(handlerId);
            return chain;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new RuntimeException("责任链不匹配");
    }


}
